let InitGame = require('./InitGame');
let PageViewBase = require('PageViewBase');
cc.Class({
  extends: cc.Component,
  properties: {
    pageViewRoot: cc.Node,
    
    
  },
  ctor() {
    this.isTutorialMode = false;
    this.pageViewNodes = {};
    this.currentPageView = null;
    this.currentPageViewName = null;
  },
  onLoad() {
    this.initGame();
  },
  onEnable() {
    if (CC_EDITOR)
      return;
    
    cc.CEventManager.instance.dispatchEvent(EVENTS.SCENE_LOADED, this);
    cc.CEventManager.instance.registerEvent(EVENTS.TUTORIAL_ALL_STEP_COMPLETED, this, 0, "onTutorialAllCompleted");
    cc.CEventManager.instance.registerEvent(EVENTS.CHANGE_TO_PAGE_VIEW, this, 0, "changePageView");
  },
  onDestroy() {
    if (CC_EDITOR)
      return;
    cc.CEventManager.instance.dispatchEvent(EVENTS.SCENE_DESTROYED, this);
    cc.CEventManager.instance.releaseAllEvents(this);
  },
  
  initGame() {
    InitGame();
  },
  
  setTutorialMode(isTutorialMode) {
    this.isTutorialMode = isTutorialMode;
  },
  
  
  onTutorialAllCompleted() {
    this.setTutorialMode(false);
  },
  
  changeToPageViewByNode(pageViewNode, data) {
    let pageViewBase;
    if (pageViewNode) {
      pageViewBase = pageViewNode.getComponent(PageViewBase);
    }
    if (this.currentPageView === pageViewNode) {
      if (pageViewBase) {
        pageViewBase.setClientData(data);
      }
      return;
    }
    let oldPageView = this.currentPageView;
    
    if (!pageViewNode) {
      console.error("异常:changeToPageViewByNode", pageViewNode);
      return;
    }
    
    if (pageViewBase) {
      pageViewBase.setClientData(data);
    }
    
    this.currentPageView = pageViewNode;
    this.currentPageView.position = cc.Vec2.ZERO;
    this.currentPageView.parent = this.pageViewRoot ? this.pageViewRoot : this.node;
    this.currentPageView.active = true;
    
    let oldPageViewBase;
    if (oldPageView) {
      oldPageView.active = false;
      oldPageViewBase = oldPageView.getComponent(PageViewBase);
    }
    if (oldPageViewBase) {
      oldPageViewBase.onPageClose();
      if (pageViewBase) {
        this.scheduleOnce(() => {
          pageViewBase.onPageOpen();
        });
      }
    } else {
      if (pageViewBase) {
        pageViewBase.onPageOpen();
      }
    }
    return pageViewNode;
  },
  
  
  changeToPageViewByPrefab(prefab, clientData) {
    if (!prefab) {
      console.error("异常:changeToPageViewByPrefab", prefab);
      return;
    }
    
    let pageViewNode = this.pageViewNodes[prefab.name];
    if (pageViewNode === undefined) {
      pageViewNode = cc.instantiate(prefab);
      this.pageViewNodes[prefab.name] = pageViewNode;
    }
    
    this.changeToPageViewByNode(pageViewNode, clientData);
    return pageViewNode;
  },
  
  /**
   * 子类需重新实现此方法, 来切换视图
   * @param pageViewName
   */
  changePageView(pageViewName) {
    if (this.currentPageViewName === pageViewName) {
      return false;
    }
    this.currentPageViewName = pageViewName;
    return true;
  },
  
  
  /**
   * 取当前pageView名
   * @returns {null|*}
   */
  getCurrentPageViewName() {
    return this.currentPageViewName;
  }
});
